<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WIKI</title>
<script>
//获取url中的参数
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }
// 用于存储用户输入的股票代码
var stockCodes = [];

// 初始化股票代码数组
window.onload = function() {
    var input = document.getElementById('stockInput');
    document.getElementById('stockInput').value=getUrlParam("code")!=null?getUrlParam("code"):"510300";
	stockCodes=document.getElementById('stockInput').value.split(',').map(function(code) {
            return code.trim();
        });
    input.addEventListener('input', function() {
        stockCodes = input.value.split(',').map(function(code) {
            return code.trim();
        });
    });
    updateIframeUrl();
};

// 当用户按下键盘上的左键或右键时触发
document.addEventListener('keydown', function(event) {
    if (event.key === 'ArrowRight') {
	    document.getElementById('indexInput').value=parseInt(document.getElementById('indexInput').value)+1;
        updateIframeUrl();
    }
	if (event.key === 'ArrowLeft') {
	    document.getElementById('indexInput').value=parseInt(document.getElementById('indexInput').value)-1;
        updateIframeUrl();
    }
});

// 更新iframe的URL
function updateIframeUrl() {
    var iframe = document.getElementById('stockIframe');
    var index = document.getElementById('indexInput').value;
    index = parseInt(index, 10) || 0; // 确保index是数字
    if (stockCodes.length > index) {
        var baseIframeUrl = "http://8.138.24.62:8080/stock_minute.html?code=";
        var newUrl = baseIframeUrl + stockCodes[index];
        iframe.src = newUrl;
        document.title = stockCodes[index];

    } else {
        console.error('Index out of range');
    }
}
</script>
</head>
<body>
<h2>请输入用逗号隔开:</h2>
<input type="text" id="stockInput" value="" placeholder="例如：AAPL,GOOGL,MSFT">
<input type="text" id="indexInput" value="0" min="0" >
<iframe id="stockIframe" src="" width="100%" height="800px"></iframe>
</body>
</html>